
package com.kugou.game.sdk.protocol;

import com.kugou.framework.base.AppException;
import com.kugou.framework.http.KGHttpClient;
import com.kugou.framework.http.RequestPackage;
import com.kugou.framework.http.ResponsePackage;
import com.kugou.game.sdk.entity.ResponseMessage;
import com.kugou.game.sdk.utils.UrlWrapper;

import org.apache.http.HttpEntity;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * 描述:请求游戏中心红点请求类
 * 
 * @author liux
 * @since 2014-11-11 上午10:56:47
 */
public class RequestGamecenterRedpointTask {
    public GameRedpointResponse request(String kugouid, String imei, int nettype, String mid,
            String uuid) {
        GameRedpointRequestPackage req = new GameRedpointRequestPackage();
        GameRedpointResponse respObj = new GameRedpointResponse();
        ResponsePackage resp = new GameRedpointResponsePackage();
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("uid", kugouid);
        params.put("imei", imei);
        params.put("nettype", nettype);
        params.put("mid", mid);
        params.put("uuid", uuid);
        req.setParams(params);
        try {
            KGHttpClient.request(req, resp);
            resp.getResponseData(respObj);
            return respObj;
        } catch (AppException e) {
        }
        return null;
    }

    static class GameRedpointRequestPackage extends AbstractRequestPackage {

        @Override
        public HttpEntity getPostRequestEntity() {
            return null;
        }

        @Override
        public String getUrl() {
            return UrlWrapper.GAMECENTER_REDPOINT_URL;
        }

        @Override
        public int getRequestType() {
            return RequestPackage.TYPE_GET;
        }

    }

    static class GameRedpointResponsePackage implements ResponsePackage<GameRedpointResponse> {

        private byte[] mData;

        @Override
        public void setContext(byte[] data) {
            mData = data;
        }

        @Override
        public void getResponseData(GameRedpointResponse response) {
            if (response != null && mData != null && mData.length > 0) {
                try {
                    String jsonStr = new String(mData, "utf-8");
                    JSONObject jsonObj = new JSONObject(jsonStr);
                    response.setResponse(jsonStr);
                    response.setOk(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public static class GameRedpointResponse extends ResponseMessage {
        private String response;

        public String getResponse() {
            return response;
        }

        public void setResponse(String response) {
            this.response = response;
        }
    }
}
